package com.atguigu.eduorder.controller;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.eduorder.entity.TOrder;
import com.atguigu.eduorder.service.TOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.OffsetDateTime;
import java.util.List;

/**
 * @author cyt
 * @create 2020-06-02 16:06
 */
@RequestMapping("/orderservice/order")
@RestController
@Slf4j
@Api(description = "订单微服务")
public class orderController {

    @Autowired
    TOrderService orderService;


    /**
     * 查询出所有的订单信息
     * @return
     */
    @GetMapping("/getOrderList")
    public R getOrderList() {
        List<TOrder> list = orderService.list(null);
        return R.ok().data("list", list);
    }

    /**
     * 创建订单
     *
     * @param courseId
     * @param request
     * @return
     */
    @PostMapping("/createOrder/{courseId}")
    public R save(@PathVariable String courseId, HttpServletRequest request) {
        String orderId = orderService.saveOrder(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("orderId", orderId);
    }


    /**
     * 返回订单的信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getOrderInfo/{id}")
    public R getOrderInfo(@PathVariable String id) {
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", id);
        TOrder one = orderService.getOne(wrapper);
        return R.ok().data("item", one);
    }

    /**
     * 根据用户的id 以及 课程的 id 查询订单信息
     *
     * @param memberId
     * @param courseId
     * @return
     */
    @GetMapping("idBuy/{memberId}/{courseId}")
    public boolean idBuy(@PathVariable("memberId") String memberId,
                         @PathVariable("courseId") String courseId) {
        int count = orderService.isBuy(memberId, courseId);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }
}
